package com.tianqu.tms.www.car.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsActionNotTimeout;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.www.car.controller.CarAutoTerminalBrandNewCarListController;
import com.tianqu.tms.www.car.viewhelper.CarAutoTerminalManageViewHelper;

/**
 * 特权车管理-初期显示
 * 
 * @author ZhaoQiChao
 *
 */
public class CarAutoTerminalBrandNewCarListAction extends TmsActionNotTimeout {
	
	private Logger logger = Logger.getLogger(CarAutoTerminalBrandNewCarListAction.class);
	private static final String BACKFLAG = "backFlag";
	public static final String TONEWCARINPUT = "toNewCarInput";
	public static final String TONEWCARDETAIL = "toNewCarDetail";
//	private static final String TONEWCARLIST1 = "toNewCarList1";
	private static final String TONEWCARLIST2 = "toNewCarList2";
//	private static final String TONEWCARLIST3 = "toNewCarList3";
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		CarAutoTerminalManageViewHelper view = new CarAutoTerminalManageViewHelper();
		if (!StringUtil.isNullOrBlank(uc.getParameter("vin"))
				&& !"1".equals(String.valueOf(uc.getAttribute(BACKFLAG)))) {
			CarAutoTerminalBrandNewCarListController controller  = new CarAutoTerminalBrandNewCarListController();
			controller.execute(uc);
			
			@SuppressWarnings("rawtypes")
			List list = (List)uc.getAttribute(TmsConstants.VIEWHELP);
			if(list == null || list.size() == 0){
				return mapping.findForward(TONEWCARINPUT);
			}
			if (list.size() == 1) {
				TransferAcceptedInfoDto infoDto = (TransferAcceptedInfoDto)list.get(0);
				view.setPreTransfer(infoDto.getPreTransfer());
				view.setTransferPrice(infoDto.getTransferPrice());
				view.setVin(infoDto.getVin());
				view.setVinNum(infoDto.getVinNum());
				view.setBackUrl("carAutoTerminalBrandNewCarInput.tq?vin=" + uc.getParameter("vin"));
				view.setPhoto_1(infoDto.getPhoto_1());
				view.setSerial(infoDto.getSerial());
				request.setAttribute(VIEWHELP, view);
				return mapping.findForward(TONEWCARDETAIL);
			}
			String serial = request.getParameter("serial");
			if (list.size() > 1 && serial != null ) {
				for (Object obj : list) {
					TransferAcceptedInfoDto infoDto = (TransferAcceptedInfoDto)obj;
					if(infoDto.getSerial().equals(serial)) {
						view.setPreTransfer(infoDto.getPreTransfer());
						view.setTransferPrice(infoDto.getTransferPrice());
						view.setVin(infoDto.getVin());
						view.setVinNum(infoDto.getVinNum());
						view.setBackUrl("carAutoTerminalBrandNewCarInput.tq?vin=" + uc.getParameter("vin"));
						view.setPhoto_1(infoDto.getPhoto_1());
						view.setSerial(infoDto.getSerial());
						request.setAttribute(VIEWHELP, view);
						return mapping.findForward(TONEWCARDETAIL);
					}
				}
			}
			view.setVinList(list);
			view.setVin(uc.getParameter("vin"));
			uc.removeAttribute(BACKFLAG);
			// “1：查验照相、2：流程受理、18：退办继续受理”
			view.setStatus(request.getParameter("status"));
			view.setType(request.getParameter("type"));
		}
		
		request.setAttribute(VIEWHELP, view);	
		
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
